﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using TmU220PrintHelperV2d0.Entity;
using TmU220PrintHelperV2d0.HttpHelper;
using TmU220PrintHelperV2d0.Service;
using ZhangLogSysV1d0;

namespace TmU220PrintHelperV2d0.Singleton
{
    public class OrderPrintMainAndDetailSingletonV3d0
    {
        private static OrderPrintMainAndDetailSingletonV3d0 _instance = null;
        private List<rms_order_print_main> _printMainList = null;
        private static readonly object syslock = new object();
        /// <summary>
        /// 单例模式
        /// </summary>
        public static OrderPrintMainAndDetailSingletonV3d0 Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (syslock)
                    {
                        if (_instance == null)
                        {
                            _instance = new OrderPrintMainAndDetailSingletonV3d0();
                        }
                    }
                }
                return _instance;
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        private OrderPrintMainAndDetailSingletonV3d0()
        {
            _printMainList = new List<rms_order_print_main>();
            LoadData();
        }
        /// <summary>
        /// 装载数据
        /// </summary>
        private void LoadData()
        {
            try
            {
                //读取status=0的记录
                //使用接口单例模式读取数据
                string sqlString = "select * from rms_order_print_main where status=0";
                string jsonString = GetDataBySQLSingletonV1d0.Instance.Run(sqlString);
                if (!string.IsNullOrEmpty(jsonString))
                {
                    //解析 反序列化
                    _printMainList = JsonConvert.DeserializeObject<List<rms_order_print_main>>(jsonString);
                    sqlString = "select * from rms_order_print_detail where parent_id in (select uid from rms_order_print_main where status=0)";
                    jsonString = GetDataBySQLSingletonV1d0.Instance.Run(sqlString);
                    var detailListAll = JsonConvert.DeserializeObject<List<rms_order_print_detail>>(jsonString);
                    foreach (var item in _printMainList)
                    {
                        string uid = item.uid;
                        var detailList = detailListAll.FindAll(x => x.parent_id == uid);
                        item.detailList.AddRange(detailList);
                    }
                }
            }
            catch (Exception ex)
            {
                SimpleLoger.Instance.Error(ex);
            }
        }
        public void ReloadData()
        {
            LoadData();
        }
        public List<rms_order_print_main> GetPrintMainList()
        {
            LoadData();
            return _printMainList;
        }
    }
}
